// pages/ykt/index.js
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        swipers:[
            {
                img:'/images/ykt/swiper.jpg'
            }
        ],
		indicatorDots:true,
        cates:[
            {
                ico:'/images/ykt/ico-aq.jpg',
                text:'安全教育',
                id:1,
            },
            {
                ico:'/images/ykt/ico-kx.jpg',
                text:'科学教育',
                id:2,
            },
            {
                ico:'/images/ykt/ico-video.jpg',
                text:'视频课程',
                id:3,
            },
            {
                ico:'/images/ykt/ico-ts.jpg',
                text:'特色课程',
                id:4,
            },
        ],
        cid:0,
        list:[
            {
                thumb:'/images/ykt/thumb1.png',
                title:'青岛市特种设备安全智慧体验中心课程1',
                post:'https://tjy.daokeyun.com//index.php/cms/p/xuting100.html'
            },
            {
                thumb:'/images/ykt/thumb2.jpg',
                title:'青岛市特种设备安全智慧体验中心课程2',
                post:'https://tjy.daokeyun.com//index.php/cms/p/xuting100.html'
            },
            {
                thumb:'/images/ykt/thumb1.png',
                title:'青岛市特种设备安全智慧体验中心课程1',
                post:'https://tjy.daokeyun.com//index.php/cms/p/xuting100.html'
            },
            {
                thumb:'/images/ykt/thumb2.jpg',
                title:'青岛市特种设备安全智慧体验中心课程2',
                post:'https://tjy.daokeyun.com//index.php/cms/p/xuting100.html'
            }
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
         
           
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.getSetting();
        this.getList();
    },
    getSetting(){
        const that = this;
        wx.request({
          url: app.globalData.apiUrl + 'api/course/index',
          method: 'POST',
          data: {
            'openid':wx.getStorageSync('openid'),
          },
          header: {
            'Content-Type': 'application/json'
          },
          success: function (res) {
            console.log(res.data)
            let response = res.data.data;
            that.setData({
                swipers:response.sliders,
                cates:response.cates
            })
          },
          fail: function () {
            
          },
          complete: function () {
          }
        })
    },
    getList(){

        const that = this;
        wx.request({
          url: app.globalData.apiUrl + 'api/course/list',
          method: 'POST',
          data: {
            'openid':wx.getStorageSync('openid'),
            'cid':that.data.cid
          },
          header: {
            'Content-Type': 'application/json'
          },
          success: function (res) {
            console.log(res.data)
            let response = res.data.data;
            that.setData({
                list:response.list,
            })
          },
          fail: function () {
            
          },
          complete: function () {
          }
        })

    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

   /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    return {
      title: '青岛市特种设备安全智慧体验中心',
      path: '/pages/ykt/index/index',
      success: function (res) {
        wx.showToast({
          title: '已转发',
          icon: 'success',
          duration: 2000
        })
      }
    }
  },
    pretreatRichText(str) {
        return str
            .replace(/\/ueditor\/net/g, `https://tjy.daokeyun.com/ueditor/net`)
            .replace(/section/g, 'div')
            .replace(/<img([\s\S]*?)(src="[^"]+")[^>]+>/g, function (all, group1, group2) {
                return `<img ${group2} style="width:100%;" />`;
            });
    },
    navigateDetail(e){
        console.log(e);
        let index = e.currentTarget.dataset.index;
        let item = this.data.list[index];
        wx.navigateTo({
            url: '/pages/ykt/detail/index?src='+encodeURIComponent(item.post),
        })
    },
    getCourse(e) {
        let cid = e.currentTarget.dataset.id;
        this.data.cid = cid;
        this.getList();
    }
})